<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="logo-circle">LT</div>
        <h1>生活轨迹记录系统</h1>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item class="remember-me">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
        <div class="login-tips">
          <p>管理员账号: admin 密码: 1</p>
          <p>普通用户: user 密码: 1</p>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

onMounted(() => {
  // 检查是否有记住的登录信息
  const savedUser = localStorage.getItem('rememberedUser')
  if (savedUser) {
    try {
      const userData = JSON.parse(savedUser)
      loginForm.username = userData.username
      loginForm.remember = true
    } catch (e) {
      console.error('读取保存的用户信息失败', e)
    }
  }
})

const handleLogin = () => {
  loginFormRef.value.validate(valid => {
    if (!valid) return
    
    loading.value = true
    
    // 简单的登录验证，账号admin或user，密码1
    if ((loginForm.username === 'admin' || loginForm.username === 'user') && loginForm.password === '1') {
      if (loginForm.remember) {
        localStorage.setItem('rememberedUser', JSON.stringify({
          username: loginForm.username
        }))
      } else {
        localStorage.removeItem('rememberedUser')
      }
      
      // 存储登录状态和角色信息
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('userInfo', JSON.stringify({
        username: loginForm.username,
        role: loginForm.username === 'admin' ? 'admin' : 'user'
      }))
      
      setTimeout(() => {
        loading.value = false
        ElMessage.success('登录成功')
        router.push('/')
      }, 800)
    } else {
      setTimeout(() => {
        loading.value = false
        ElMessage.error('用户名或密码错误')
      }, 800)
    }
  })
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 400px;
  padding: 40px;
  border-radius: 8px;
  background-color: white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-circle {
  width: 80px;
  height: 80px;
  margin: 0 auto 15px;
  background-color: #1e5eff;
  color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
  font-weight: bold;
}

.login-header h1 {
  font-size: 24px;
  color: #1e5eff;
  margin: 0;
}

.login-form {
  margin-top: 30px;
}

.remember-me {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  background-color: #1e5eff;
}

.login-tips {
  margin-top: 15px;
  text-align: center;
  color: #909399;
  font-size: 13px;
}

.login-tips p {
  margin: 5px 0;
}
</style> 